xseat = {p:{},f:{}};
xseat.p.seat_w = 22;

xseat.p.icon_incart = 'in_cart.jpg';

xseat.f.init = function(width){
	xseat.p.checkOutWidth = 200;
	xseat.p.$win = $('#seat_windows');
	xseat.p.$overlay = $('#overlay');
	xseat.p.$loading = $('#loading');
	xseat.p.$bar = $('#seat_bar');
	xseat.p.$checkout = $('#seat_checkout');
	xseat.p.$seat_area = $('#seat_area');
	xseat.p.$seat_map = $('#seat_map_container');
	xseat.p.$cart_seat = $('#cart_tickets');
	xseat.p.seat_ticket_id = xseat.p.$win.data('seat_ticket_id');
	xseat.p.ticket_showing_id = xseat.p.$win.data('id');
	xseat.f.bindEvent();
	xseat.f.ready();
	xseat.p.seat_w = width;
};

xseat.f.bindEvent = function(){
	//xseat.f.overlayHide();
	//xseat.f.dragable();
	jQuery('.seat_table_hander',xseat.p.$bar).click(function(){
		 xseat.f.loadTable($(this).data('id'));
	});
	
	jQuery('#zoom_bar .zoom_in').click(function(){
		xseat.f.zoomIn();
	});
	
	jQuery('#zoom_bar .zoom_out').click(function(){
		xseat.f.zoomOut();
	});
	
	$('#cart_submit').click(function(){
		xseat.f.submit();
	});
};


xseat.f.ready = function(){
	$('#seat_bar .seat_table a:first').click();
};

xseat.f.dragArea = function(){
	xseat.p.$seat_map.width((Number(xseat.p.data.w)+1)*xseat.p.seat_w);
	xseat.p.$seat_map.height((Number(xseat.p.data.h)+1)*xseat.p.seat_w);
	
	var fixw = xseat.p.$bar.width();
	var saw = xseat.p.$seat_map.width()*2-100;
	saw += xseat.p.$win.width() - fixw;
	var sah = xseat.p.$seat_map.height()*2-100;
	sah += xseat.p.$win.height();
	
	var sat = -xseat.p.$seat_map.height()+50;
	var sal = -xseat.p.$seat_map.width()+50;
	
	cl = (saw - xseat.p.$seat_map.width())/2;
	ct = (sah - xseat.p.$seat_map.height())/2;
	xseat.p.$seat_map.css({top:ct+180,left:cl});
	xseat.p.$seat_area.css({
		width:saw,
		height:sah,
		top:sat,
		left:sal+fixw
	});
};

xseat.f.zoomIn = function(){
	xseat.p.seat_w+=2;
	if(xseat.p.seat_w>26){
		xseat.p.seat_w = 26;
	}
	xseat.f.dragArea();
	jQuery('#seat_map .seat ul').each(function(){
		var li_count = jQuery(this).find('>li').size();
		$(this).css({
			width:li_count*xseat.p.seat_w,
			height:xseat.p.seat_w,
			left:$(this).data('pos-x')*xseat.p.seat_w,
			top:$(this).data('pos-y')*xseat.p.seat_w
		});
		$(this).find('li').css({width:xseat.p.seat_w,height:xseat.p.seat_w});
	});
};
xseat.f.zoomOut = function(){
	xseat.p.seat_w-=2;
	if(xseat.p.seat_w<10){
		xseat.p.seat_w = 10;
	}
	xseat.f.dragArea();
	jQuery('#seat_map .seat ul').each(function(){
		var li_count = jQuery(this).find('>li').size();
		$(this).css({
			width:li_count*xseat.p.seat_w,
			height:xseat.p.seat_w,
			left:$(this).data('pos-x')*xseat.p.seat_w,
			top:$(this).data('pos-y')*xseat.p.seat_w
		})
		$(this).find('li').css({width:xseat.p.seat_w,height:xseat.p.seat_w});
	});
};

xseat.f.dragable = function(data){
	xseat.p.data = data;
	xseat.f.dragArea(data);
	xseat.p.$seat_map.draggable({
		containment:'#seat_area'
	});
};


xseat.f.loadTable = function(table_id){
	var data = {
		type:'GET',
		action:'seat/SeatSolds/seatTable/'+table_id+'/'+xseat.p.seat_ticket_id+'/'+xseat.p.ticket_showing_id
	};
	xseat.f.action(data,xseat.f.loadTableCallback);
};
xseat.f.loadTableCallback = function(res){
	if(res.state){
		xseat.p.$seat_map.html(res.html);
		xseat.f.dragable(res.data);
		xseat.f.tip();
		xseat.f.checkoutEvent();
		xseat.f.seatState();
	}else{
		alert(res.msg);
	}
};

xseat.f.tip = function(){
	$( '.sell_seat',xseat.p.$seat_map ).tooltip({
      position: {
        my: "center bottom-20",
        at: "center top",
        using: function( position, feedback ) {
          $( this ).css( position );
          $( "<div>" )
            .addClass( "arrow" )
            .addClass( feedback.vertical )
            .addClass( feedback.horizontal )
            .appendTo( this );
        }
      }
    });
};

xseat.f.checkoutShow = function(){
	xseat.p.$checkout.animate({right:0});
};
xseat.f.checkoutHide = function(){
	xseat.p.$checkout.animate({right:-xseat.p.checkOutWidth});
};
xseat.f.checkoutEmpty = function(){};
xseat.f.checkoutAdd = function(html){
	xseat.p.$cart_seat.find('tbody').append(html);
	xseat.p.checkoutState();
};
xseat.f.checkoutDel = function(id){
	xseat.p.$cart_seat.find('tbody tr.seat_id_'+id).remove();
	xseat.p.checkoutState();
};

xseat.f.checkoutEvent = function(){
	$( '.sell_seat',xseat.p.$seat_map ).click(function(){
		xseat.f.addToCheckout(this);
	});
};

xseat.p.checkoutState = function(){
	var count = xseat.p.$cart_seat.find('tbody tr').size();
	jQuery('#cart_count_').html(count);
	
	var total = 0;
	xseat.p.$cart_seat.find('tbody tr').each(function(){
		total+=$(this).data('price');
	});
	jQuery('#cart_total').html(total);
	if(count == 1){
		xseat.f.checkoutShow();
	}else if(count == 0){
		xseat.f.checkoutHide();
	}
	xseat.f.seatState();
};

xseat.p.checkout = {};

xseat.f.addToCheckout = function(tar){
	var st = {}; 
	st.seat_id = $(tar).data('seat');
	if(!xseat.f.checkCanBuy(st.seat_id)){
		xseat.f.checkoutDel(st.seat_id);
		return;
	}
	st.show_title = $(tar).data('uiTooltipTitle');
	st.ticket_showing_id = xseat.p.ticket_showing_id;
	st.seat_type_id = $(tar).data('seat_type_id');
	st.seat_type = $(tar).data('seat_type');
	st.price = $(tar).data('price');

	var html = '<tr data-id = "'+st.seat_id+'" data-title = "'+st.show_title+'" data-price = "'+st.price+'" data-name = "'+st.seat_type.name+'" class = "seat_select seat_id_'+st.seat_id+'">';
	html+= '<td >'+st.show_title+'</td>';
	html+= '<td >'+st.seat_type.name+'</td>';
	html+= '<td class="st_prict td_right" >'+st.price+'</td>';
	html+= '<td class = "st_op">';
	html+= '<a href = "javascript:xseat.f.checkoutDel('+st.seat_id+');">X</a>';
	html+= '</td>';
	html+= '</tr>';
	
	xseat.f.checkoutAdd(html);
};

xseat.f.seatState = function(){
	$('.seat_td',xseat.p.$seat_map).removeClass('in_cart');
	
	$('.seat_td img',xseat.p.$seat_map).each(function(){
		$(this).attr('src','seat/img/seat/'+$(this).data('icon'));
	});
	
	$('tbody tr',xseat.p.$cart_seat).each(function(){
		var seat_id = $(this).data('id');
		jQuery('.seat_id_'+seat_id,xseat.p.$seat_map).addClass('in_cart')
			.find('img').attr('src','seat/img/seat/'+xseat.p.icon_incart);
	});
};

xseat.f.checkCanBuy = function(id){
	if($('.seat_id_'+id,xseat.p.$cart_seat).size()){
		return false;
	}else{
		return true;
	}
};

xseat.f.submit = function(){
	var cdata = {SeatSold:[]};
	cdata.ticket_id = jQuery('#seat_windows').data('ticket_id');
	cdata.ticket_showing_id = jQuery('#seat_windows').data('id');
	cdata.seat_ticket_id = jQuery('#seat_windows').data('seat_ticket_id');
	jQuery('#cart_tickets tbody tr').each(function(i,v){
		var tmp = {
			ticket_showing_id:cdata.ticket_showing_id,
			seat_id:$(this).data('id'),
			title:$(this).data('title'),
			name:$(this).data('name'),
			price:$(this).data('price'),
			sold_out_state_id:1
		};
		cdata.SeatSold.push(tmp);
	});
	var data = {
		data:cdata,
		action:'seat/SeatSolds/seatCart'
	};
	xseat.f.action(data,xseat.f.submitCallback);
};

xseat.f.submitCallback = function(res){
	console.log(res.msg);
};

xseat.f.overlayShow = function(){
	xseat.p.$overlay.show();
	xseat.p.$loading.show();
};
xseat.f.overlayHide = function(){
	xseat.p.$loading.hide();
	xseat.p.$overlay.hide();
};

xseat.p.loading = false;
xseat.f.action = function(data,callback,errCallback){
	if(xseat.p.loading)return ;
	xseat.p.loading = true;
	xseat.f.overlayShow();
	var action = data.action;
	jQuery.ajax({
		type:data.type?data.type:'POST',
		url:data.action,
		data:data.data,
		dataType:'json',
		success : function(res){
			xseat.p.loading = false;
			callback(res);
		},
		error: function (data, status, e){
			xseat.p.loading = false;
			if(errCallback){
				errCallback();
			}
		},
		complete:function(){
			//xseat.p.loading = false;
			xseat.f.overlayHide();
		}
	});
};

